package com.example.doctorstation.POJOController;
import com.example.doctorstation.Config.MyResponse;
import com.example.doctorstation.Helper.StringHelper;
import com.example.doctorstation.POJOData.Bed;
import com.example.doctorstation.POJOMapper.BedMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
public class BedController {
    //注入UserMapper 这里DEA可能会提示报错不用理会就行
    @Autowired
    BedMapper bedMapper;

    //前端地址为：/bed?departmentName="XXXX"
    @GetMapping(value = "/bed")
    public MyResponse getAllBedsByDepartName(Bed bed) {
        try {
            if (!StringHelper.isNullOrEmpty(bed.getDepartmentName())) {
                return MyResponse.success("你拿到了所有病床信息！", bedMapper.getBed(bed.getDepartmentName()));
            }
            if (bed.getDoctorID() != null) {
                return MyResponse.success("你拿到了所有空闲病人！", bedMapper.getFreePatients(bed.getDoctorID()));
            }
            return new MyResponse();
        } catch (Throwable e) {
            e.printStackTrace();
            return MyResponse.error("该病区没有病床或空闲病人！", e.getMessage());
        }
    }


    //前端地址为：/bed/XXXX，restful风格，中文参数要转Unicode编码
    @GetMapping(value = "/bed/{departmentName}")
    public MyResponse getAllBedsByDepartName1(@PathVariable("departmentName") String departmentName) {
        try {
            return MyResponse.success("你拿到了所有病床信息！", bedMapper.getBed(departmentName));
        } catch (Throwable e) {
            e.printStackTrace();
            return MyResponse.error("该病区没有空闲病床！", e.getMessage());
        }
    }

    @PutMapping(value="/bed")
    public MyResponse UpdateBed(@RequestBody Bed bed) {
        try {
            Bed tempbed = bedMapper.SelectBedInfo(bed.getBedSeq(),bed.getDepartmentName());
            if(tempbed == null)
            {
                return MyResponse.error("该床位不存在！", null);
            }
            //被占用
            if(tempbed.getInHospitalInfoID() != -1)
            {
                return MyResponse.error("该床位被占用，请选择其他床位！", null);
            }
            else
            {
                int resValue = bedMapper.UpdateBedInfoNotOut(bed.getBedSeq(),bed.getDepartmentName(),bed.getInHospitalInfoID());
                return new MyResponse(resValue, "已将病人转移到该床位", "转移床位失败！");
            }
        } catch (Throwable e) {
            e.printStackTrace();
            return new MyResponse();
        }
    }
}
